//
//  BPCommon.m
//  BP_NewClient
//
//  Created by normal on 15-1-25.
//  Copyright (c) 2015年 normal. All rights reserved.
//

#import "BPCommon.h"
#import "Constants.h"
#import "PinYinForObjc.h"
#import "ChineseToPinyin.h"
#import "HanyuPinyinOutputFormat.h"
#import "PinyinHelper.h"
#import "CityModel.h"

@implementation BPCommon
+ (UIButton *)addButton:(CGRect)rect title:(NSString *)title backGround:(NSString *)imgString setTag:(NSInteger)tag setId:(id)_sel selector:(SEL)selector setFont:(UIFont *)font setTextColor:(UIColor *)color{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = rect;
    [btn setBackgroundImage:[UIImage imageNamed:imgString] forState:UIControlStateNormal];
    [btn setTag:tag];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn.titleLabel setFont:font];
    [btn setTitleColor:color forState:UIControlStateNormal];
    [btn addTarget:_sel action:selector forControlEvents:UIControlEventTouchUpInside];
    
    return btn;
}

+ (UIView *)addBackView:(CGRect)rect setBackColor:(UIColor *)color{
    UIView *view = [[UIView alloc] initWithFrame:rect];
    view.backgroundColor = color;
    return view;
}

+ (UIImageView *)addImageView:(CGRect)rect setImage:(NSString *)imageString{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect];
    //    [imageView setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:nil];
    imageView.image = [UIImage imageNamed:imageString];
    return imageView;
}

+ (UILabel *)addLabel:(CGRect)rect setTitle:(NSString *)title setBackColor:(UIColor *)color setFont:(UIFont *)font{
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.text = title;
    label.numberOfLines = 0;
    label.font = font;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = color;
    return label;
}


//压缩图片
+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    // End the context
    UIGraphicsEndImageContext();
    // Return the new image.
    return newImage;
}

#pragma mark 保存图片到document
+ (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName
{
    NSData* imageData = UIImageJPEGRepresentation(tempImage, 1.0);
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];
    // Now we get the full path to the file
    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
    // and then we write it out
    [imageData writeToFile:fullPathToFile atomically:NO];
}

//通过十六进制和alpha生成颜色
+ (UIColor*)colorWithHexValue:(NSInteger)aHexValue
                        alpha:(CGFloat)aAlpha {
    return [UIColor colorWithRed:((CGFloat)((aHexValue&0xFF0000)>>16))/255.0
                           green:((CGFloat)((aHexValue&0xFF00)>>8))/255.0
                            blue:((CGFloat)(aHexValue&0xFF))/255.0
                           alpha:aAlpha];
    
}

// 计算UILabl文本高度
+ (CGFloat)getLabelHeight:(NSString *)labelString setLabelWidth:(CGFloat)labelwidth setFont:(UIFont *)font{
    CGFloat height = 0.0f;
    if ([labelString isMemberOfClass:[NSNull class]]) {
        return height;
    }else{
        CGSize labelSize = [labelString sizeWithFont:font constrainedToSize:CGSizeMake(labelwidth, 1000) lineBreakMode:NSLineBreakByCharWrapping];
        height = labelSize.height;
    }
    return height;
}

+ (void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension {
    if ([[extension lowercaseString] isEqualToString:@"png"]) {
        [UIImagePNGRepresentation(image) writeToFile:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
    } else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
        [UIImageJPEGRepresentation(image, 1.0) writeToFile:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
    } else {
        //ALog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
        NSLog(@"文件后缀不认识");
    }
}

+ (NSString *)getImagePath:(NSString *)imgName Type:(NSString *)type{
    
    NSString *path = [NSString stringWithFormat:@"%@",[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingString:[NSString stringWithFormat:@"/%@.%@", imgName,type]]];
    
    return path;
}

+ (BOOL)isRetina
{
    int scale = (int)[[UIScreen mainScreen] scale];
    return (scale > 1);
}

// 将城市数组转换 － a-z Section
+ (NSMutableArray *)convertToSectionArray:(NSArray *)originDataArray HotCityArray:(NSMutableArray *)hotCity{
    NSMutableArray *sectionArray= [[NSMutableArray alloc] init];
    // 格式化数组 热门－a-z
    for(int i = 0; i < CITY_FIRST_LETTER.length; i ++) {
        [sectionArray addObject:[NSMutableArray array]];
    }
    // 定位城市
    [[sectionArray objectAtIndex:0] removeAllObjects];
    // 热门城市
//    [[sectionArray objectAtIndex:1] addObjectsFromArray:hotCity];
    
    // 字母_a~z姓名排序
    for (int i = 0; i < [originDataArray count]; i ++) {
        
        CityModel *model = originDataArray[i];
        
        NSString *cityName = model.region_name;
        //首字字母分组
//        NSString *firstCityNameLetter = [[NSString stringWithFormat:@"%c",pinyinFirstLetter([cityName characterAtIndex:0])] uppercaseString];
//        
        
       NSString *firstCityNameLetter = [[[ChineseToPinyin pinyinFromChiniseString:cityName] substringToIndex:1] uppercaseString];
//        NSLog(@"firstCityNameLetter is %@",firstCityNameLetter);
      
        NSUInteger firstLetterLocation;
        firstLetterLocation = [CITY_FIRST_LETTER rangeOfString:firstCityNameLetter].location;
      
        if ([cityName isEqualToString:@"重庆"] || [cityName isEqualToString:@"长乐"]) {
            firstLetterLocation = [CITY_FIRST_LETTER rangeOfString:@"C"].location;
        }
        if ([cityName isEqualToString:@"厦门"]) {
            firstLetterLocation = [CITY_FIRST_LETTER rangeOfString:@"X"].location;
        }
        if (firstLetterLocation != NSNotFound){
            [[sectionArray objectAtIndex:firstLetterLocation] addObject:[originDataArray objectAtIndex:i]];
        }
    }
    return sectionArray;
}


@end
